package com.byapp.fast.common.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class LogInterceptor implements HandlerInterceptor {


    public LogInterceptor() {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contentType = request.getContentType();
        StringBuffer url = request.getRequestURL();
        String method = request.getMethod();
        if (MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(contentType)) {
            String param = IOUtils.toString(request.getInputStream(), "UTF-8");
            log.info("【日志】请求的地址={}，方法类型={}，参数体={}", url, method, param);
        } else {
            log.info("【日志】请求的地址={}，方法类型={}", url, method);
        }
        return true;
    }


}
